﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BLL.BatchBLL;
using Model;

namespace MDMUI
{
    public partial class unCrteProdBatch : Form
    {
        private unCrteProdBatchBLL batchBll = new unCrteProdBatchBLL(); // 创建 BLL 层实例
        private List<QueryBatchAndWorkOrderItem> currentData = new List<QueryBatchAndWorkOrderItem>(); // 用于存储当前查询结果的列表

        public unCrteProdBatch()
        {
            InitializeComponent();
            InitializeDataGridView(); // 初始化 DataGridView 组件
        }

        private void InitializeDataGridView()
        {
            // 清空现有列
            dataGridView1.Columns.Clear();

            // 设置 DataGridView 的属性
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ReadOnly = true; // 设置为只读，防止用户修改数据
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 设置选择模式为整行选择

            // 添加列
            // 批次号
            DataGridViewTextBoxColumn colBatchId = new DataGridViewTextBoxColumn();
            colBatchId.Name = "BatchId";
            colBatchId.HeaderText = "批次号";
            colBatchId.DataPropertyName = "BatchId";
            colBatchId.Width = 80;
            dataGridView1.Columns.Add(colBatchId);

            // 批次类型
            DataGridViewTextBoxColumn colBatchType = new DataGridViewTextBoxColumn();
            colBatchType.Name = "BatchType";
            colBatchType.HeaderText = "批次类型";
            colBatchType.DataPropertyName = "BatchType";
            colBatchType.Width = 80;
            dataGridView1.Columns.Add(colBatchType);

            // 单位
            DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
            colUnit.Name = "Unit";
            colUnit.HeaderText = "单位";
            colUnit.DataPropertyName = "Unit";
            colUnit.Width = 60;
            dataGridView1.Columns.Add(colUnit);

            // 批次数量
            DataGridViewTextBoxColumn colBatchQuantity = new DataGridViewTextBoxColumn();
            colBatchQuantity.Name = "Quantity";
            colBatchQuantity.HeaderText = "批次数量";
            colBatchQuantity.DataPropertyName = "Quantity";
            colBatchQuantity.Width = 80;
            dataGridView1.Columns.Add(colBatchQuantity);

            // 子产品数量
            DataGridViewTextBoxColumn colSubProductQuantity = new DataGridViewTextBoxColumn();
            colSubProductQuantity.Name = "SubQuantity";
            colSubProductQuantity.HeaderText = "子产品数量";
            colSubProductQuantity.DataPropertyName = "SubQuantity";
            colSubProductQuantity.Width = 100;
            dataGridView1.Columns.Add(colSubProductQuantity);

            // 在制品状态
            DataGridViewTextBoxColumn colWorkInProcessStatus = new DataGridViewTextBoxColumn();
            colWorkInProcessStatus.Name = "LockStatus";
            colWorkInProcessStatus.HeaderText = "在制品状态";
            colWorkInProcessStatus.DataPropertyName = "LockStatus";
            colWorkInProcessStatus.Width = 100;
            dataGridView1.Columns.Add(colWorkInProcessStatus);

            // 投产状态
            DataGridViewTextBoxColumn colLockStatus = new DataGridViewTextBoxColumn();
            colLockStatus.Name = "LockStatus";
            colLockStatus.HeaderText = "投产状态";
            colLockStatus.DataPropertyName = "LockStatus";
            colLockStatus.Width = 80;
            dataGridView1.Columns.Add(colLockStatus);

            // 工单号
            DataGridViewTextBoxColumn colWorkOrderNumber = new DataGridViewTextBoxColumn();
            colWorkOrderNumber.Name = "WorkOrderNumber";
            colWorkOrderNumber.HeaderText = "工单号";
            colWorkOrderNumber.DataPropertyName = "WorkOrderNumber";
            colWorkOrderNumber.Width = 80;
            dataGridView1.Columns.Add(colWorkOrderNumber);

            // 产品编号
            DataGridViewTextBoxColumn colProductNumber = new DataGridViewTextBoxColumn();
            colProductNumber.Name = "ProductId";
            colProductNumber.HeaderText = "产品编号";
            colProductNumber.DataPropertyName = "ProductId";
            colProductNumber.Width = 80;
            dataGridView1.Columns.Add(colProductNumber);

            // 工艺流程号
            DataGridViewTextBoxColumn colProcessNumber = new DataGridViewTextBoxColumn();
            colProcessNumber.Name = "TotalProcessId";
            colProcessNumber.HeaderText = "工艺流程号";
            colProcessNumber.DataPropertyName = "TotalProcessId";
            colProcessNumber.Width = 100;
            dataGridView1.Columns.Add(colProcessNumber);

            // 工艺流程版本
            DataGridViewTextBoxColumn colProcessVersion = new DataGridViewTextBoxColumn();
            colProcessVersion.Name = "ProcessVersion";
            colProcessVersion.HeaderText = "工艺流程版本";
            colProcessVersion.DataPropertyName = "ProcessVersion";
            colProcessVersion.Width = 100;
            dataGridView1.Columns.Add(colProcessVersion);

            // 工站号
            DataGridViewTextBoxColumn colWorkstationNumber = new DataGridViewTextBoxColumn();
            colWorkstationNumber.Name = "WorkstationNumber";
            colWorkstationNumber.HeaderText = "工站号";
            colWorkstationNumber.DataPropertyName = "WorkstationNumber";
            colWorkstationNumber.Width = 80;
            dataGridView1.Columns.Add(colWorkstationNumber);

            // 工站版本
            DataGridViewTextBoxColumn colWorkstationVersion = new DataGridViewTextBoxColumn();
            colWorkstationVersion.Name = "WorkstationVersion";
            colWorkstationVersion.HeaderText = "工站版本";
            colWorkstationVersion.DataPropertyName = "WorkstationVersion";
            colWorkstationVersion.Width = 80;
            dataGridView1.Columns.Add(colWorkstationVersion);

            // 描述
            DataGridViewTextBoxColumn colDescription = new DataGridViewTextBoxColumn();
            colDescription.Name = "Description";
            colDescription.HeaderText = "描述";
            colDescription.DataPropertyName = "Description";
            colDescription.Width = 150;
            dataGridView1.Columns.Add(colDescription);

            // 工厂
            DataGridViewTextBoxColumn colFactory = new DataGridViewTextBoxColumn();
            colFactory.Name = "Factory";
            colFactory.HeaderText = "工厂";
            colFactory.DataPropertyName = "Factory";
            colFactory.Width = 80;
            dataGridView1.Columns.Add(colFactory);

            // 事件发生时间
            DataGridViewTextBoxColumn colEventTime = new DataGridViewTextBoxColumn();
            colEventTime.Name = "EventTime";
            colEventTime.HeaderText = "事件发生时间";
            colEventTime.DataPropertyName = "EventTime";
            colEventTime.Width = 120;
            dataGridView1.Columns.Add(colEventTime);

            // 事件用户
            DataGridViewTextBoxColumn colTimeUser = new DataGridViewTextBoxColumn();
            colTimeUser.Name = "EventUser";
            colTimeUser.HeaderText = "事件用户";
            colTimeUser.DataPropertyName = "EventUser";
            colTimeUser.Width = 80;
            dataGridView1.Columns.Add(colTimeUser);

            // 事件备注
            DataGridViewTextBoxColumn colEventRemark = new DataGridViewTextBoxColumn();
            colEventRemark.Name = "EventRemarks";
            colEventRemark.HeaderText = "事件备注";
            colEventRemark.DataPropertyName = "EventRemarks";
            colEventRemark.Width = 100;
            dataGridView1.Columns.Add(colEventRemark);

            // 事件
            DataGridViewTextBoxColumn colEvent = new DataGridViewTextBoxColumn();
            colEvent.Name = "EventName";
            colEvent.HeaderText = "事件名称";
            colEvent.DataPropertyName = "EventName";
            colEvent.Width = 80;
            dataGridView1.Columns.Add(colEvent);

            // 创建时间
            DataGridViewTextBoxColumn colCreateTime = new DataGridViewTextBoxColumn();
            colCreateTime.Name = "CreateTime";
            colCreateTime.HeaderText = "创建时间";
            colCreateTime.DataPropertyName = "CreateTime";
            colCreateTime.Width = 120;
            dataGridView1.Columns.Add(colCreateTime);

            // 创建用户
            DataGridViewTextBoxColumn colCreateUser = new DataGridViewTextBoxColumn();
            colCreateUser.Name = "CreateUser";
            colCreateUser.HeaderText = "创建用户";
            colCreateUser.DataPropertyName = "CreateUser";
            colCreateUser.Width = 80;
            dataGridView1.Columns.Add(colCreateUser);
        }

        private void unCrteProdBatch_Load(object sender, EventArgs e)
        {
            try
            {
                // 调用 BLL 层的方法获取数据
                currentData = batchBll.QueryBatchAndWorkOrder();

                // 过滤数据，只显示投产状态为“已投产”的数据
                var filteredData = currentData.Where(item => item.LockStatus == "已投产").ToList();

                // 检查是否有数据
                if (filteredData.Count == 0)
                {
                    MessageBox.Show("没有找到已投产的批次数据。");
                }

                // 将查询结果显示在 DataGridView 中
                dataGridView1.DataSource = filteredData;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取数据时发生错误: {ex.Message}");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 调用 BLL 层的方法获取数据
                currentData = batchBll.QueryBatchAndWorkOrder();

                // 过滤数据，只显示投产状态为“已投产”的数据
                var filteredData = currentData.Where(item => item.LockStatus == "已投产").ToList();

                // 检查是否有数据
                if (filteredData.Count == 0)
                {
                    MessageBox.Show("没有找到已投产的批次数据。");
                }

                // 将查询结果显示在 DataGridView 中
                dataGridView1.DataSource = filteredData;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取数据时发生错误: {ex.Message}");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 清空输入框
            textBox1.Clear();

            // 清空 DataGridView
            dataGridView1.DataSource = null;
        }
    }
}